package com.gold.web;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import com.alibaba.fastjson.JSONObject;
import com.gold.model.PurchaseGold;
import com.gold.service.CustomerService;
import com.gold.service.GoldService;
import com.gold.service.PurchaseGoldService;
import com.gold.util.BaseController;
import com.gold.util.GridPage;
import com.gold.util.Page;


/**
 * Created by wxf
 * 员工登录
 */
@Controller
@RequestMapping(value="acquisitionGold")
public class AcquisitionGoldContorller extends BaseController{

    @Resource
    private PurchaseGoldService purchaseGoldService;
    @Resource
    private CustomerService customerService;
    @Resource
    private GoldService goldService;
    
    @RequestMapping(value="")
    public ModelAndView toPurchaseVIew(HttpServletRequest request) {
		ModelAndView mo=new ModelAndView();
		mo.setViewName("purchaseGold");
		return mo;
	}
    //添加收购黄金的页面
    @RequestMapping(value="purchaseGoldAdd")
    public ModelAndView toPurchaseAddVIew(HttpServletRequest request) {
    	ModelAndView mo=new ModelAndView();
    	
    	int id=this.getParamInt(request, "id", 0);
    	String cId = request.getParameter("c_Id");
    	String operator = request.getParameter("operator");//1修改-----------0添加
    	if(id>0){//说明是修改
    		PurchaseGold pg=purchaseGoldService.load(id);
    		mo.addObject("purchase",net.sf.json.JSONObject.fromObject(pg));
    	}
    	
		mo.setViewName("purchase/purchase");
		mo.addObject("pgNumber", this.getCode());
		mo.addObject("golds",goldService.findAll());
		mo.addObject("customers",customerService.findAll());
		mo.addObject("cId",cId);
		mo.addObject("operator",operator);
		return mo;
	}
    
    @RequestMapping(value="/save")
    @ResponseBody
    public JSONObject save(HttpServletRequest request,PurchaseGold purchaseGold) {
		JSONObject jo=new JSONObject();
		
		boolean b=false;
		try {
			if(purchaseGold.getPgId()==null){//说明是添加
				Date date = new Date();
				purchaseGold.setPgTime(date);//添加时加上时间
				purchaseGold.setPgUpdate(date);
				purchaseGold.setPgState(0);//添加是状态默认为0
				String pay = purchaseGold.getPgPay()==null?"":purchaseGold.getPgPay().trim();
				double dpay = "".equals(pay)?0:Double.parseDouble(pay);
				String ower = String.valueOf(purchaseGold.getPgCost().doubleValue()-dpay);
				purchaseGold.setPgOwe(ower);
				b=purchaseGoldService.save(purchaseGold);
			}else if(purchaseGold.getgId()>0){//说明是修改
				purchaseGold.setPgUpdate(new Date());
				String pay = purchaseGold.getPgPay()==null?"":purchaseGold.getPgPay().trim();
				double dpay = "".equals(pay)?0:Double.parseDouble(pay);
				String ower = String.valueOf(purchaseGold.getPgCost().doubleValue()-dpay);
				purchaseGold.setPgOwe(ower);
				b=purchaseGoldService.update(purchaseGold);
			}	
			jo.put("success", b);
			if(b){
				jo.put("msg","保存成功");
			}else{
				jo.put("msg","保存失败");
			}
		} catch (Exception e) {
			jo.put("msg", "发生了异常");
		}
		return jo;
	}
    
    //huilay 2016年2月21日15:14:15 加载表格内容
    @RequestMapping("/purchaseGoldGrid")
	@ResponseBody
	public GridPage<Map> getGrid(HttpServletRequest request){
		int page=this.getParamInt(request, "page", 1);
		int rows=this.getParamInt(request, "pageSize", 10);
		Page<Map> rtPage=new Page<Map>();
		Map<String,String> queryParam=new HashMap<String,String>();
		queryParam.put("cName", request.getParameter("cName"));
		queryParam.put("gName", request.getParameter("gName"));
		queryParam.put("beginDate", request.getParameter("beginDate"));
		queryParam.put("endDate", request.getParameter("endDate"));
        rtPage.setQueryParam(queryParam);
        rtPage.setPageSize(rows);
        rtPage.setPageNo(page);
        rtPage=purchaseGoldService.findByCondition(rtPage);
		return new GridPage<Map>(rtPage);
	}
    
    
    @RequestMapping(value="/getAll")
    @ResponseBody
    public Map<String, List> getAll(){
    	Map<String, List> map=new HashMap<String,List>();
    	List<PurchaseGold> list=purchaseGoldService.findAll();
    	map.put("Rows", list);
    	return map;
    }
    
    //获取收购单号
    public String getCode(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return  "SG"+sdf.format(new Date());
    }
}
